﻿using System.Configuration;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace System
{
    public static class MyExtensions
    {

        /// <summary>
        /// 异常错误信息转换为Web.config配置语言
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static string ToMsg(this Exception e)
        {
            string ErrorMessage = e.Message;

            string ErrorMessageFilePath = ConfigurationManager.AppSettings["ErrorMessageFilePath"];

            string ErrorMessageLanguage = ConfigurationManager.AppSettings["ErrorMessageLanguage"];

            if (!string.IsNullOrEmpty(ErrorMessageFilePath))
            {
                XDocument doc = XDocument.Load(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + ErrorMessageFilePath);

                var query = doc.Element("root").Elements("error").Where(a => a.Element("Message").Value == e.Message && a.Element("Language").Value == ErrorMessageLanguage);

                if (query != null)
                {
                    if (query.Elements("Return") != null)
                    {
                        if (query.Elements("Return").FirstOrDefault()!=null)
                        {
                            ErrorMessage = query.Elements("Return").FirstOrDefault().Value;
                        }
                    }
                }
            }

            return ErrorMessage;
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static string CutString(this string inputString, int len)
        {
            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            byte[] s = ascii.GetBytes(inputString);
            for (int i = 0; i < s.Length; i++)
            {
                if ((int)s[i] == 63)
                {
                    tempLen += 2;
                }
                else
                {
                    tempLen += 1;
                }
                try
                {
                    tempString += inputString.Substring(i, 1);
                }
                catch
                {
                    break;
                }
                if (tempLen > len)
                    break;
            }
            //如果截过则加上半个省略号 
            byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
            if (mybyte.Length > len)
                tempString += "...";

            return tempString;
        }

        /// <summary>
        /// 去除字符串中的网页代码
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns></returns>
        public static string removeHtml(this string strHtml)
        {
            Regex objRegExp = new Regex("<(.|\n)+?>");
            string strOutput = objRegExp.Replace(strHtml, "");
            strOutput = strOutput.Replace("<", "&lt;");
            strOutput = strOutput.Replace(">", "&gt;");

            Regex r = new Regex(@"\s+");
            strOutput = r.Replace(strOutput, "");

            strOutput = strOutput.Replace("&nbsp;", "");
            strOutput.Trim();

            return strOutput;
        }

        /// <summary>
        /// 验证是否为数字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsUint(this string input)
        {
            Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
            return regex.IsMatch(input);
        }

        /// <summary>
        /// 验证是否为E-mail
        /// </summary>
        /// <param name="str_Email"></param>
        /// <returns></returns>
        public static bool IsEmail(string str_Email)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str_Email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }
    }


}
